<?php
class Module extends AdminController {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->_data["loadPage"]="module/index_view02";
        $this->_data["titlePage"]="Quản lý module";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->session->unset_userdata("sess_search_name");
        
        if($this->uri->segment(4)!=null) {
            $module_parent=$this->uri->segment(4);
            $this->_data["module_parent"]=$module_parent;
        }
        $this->load->model("mmodule");
        $this->load->helper("module_tree_helper");
        $tmp=$this->mmodule->listAll();
        if($tmp!=false) $this->_data["info"]=$tmp;
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function index_bak() {
        $this->_data["loadPage"]="module/index_view";
        $this->_data["titlePage"]="Quản lý module";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->session->unset_userdata("sess_search_name");
        
        $this->load->model("mmodule");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/module/index/",
            "total_rows"=>$this->mmodule->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mmodule->listAllLimit($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mmodule->listAllLimit($config["per_page"], $start);
            //$this->_data["info"]=$this->muser->listAll();
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function add() {
        $name=$this->input->post("name");
        $note=$this->input->post("note");
        $order=$this->input->post("order");
        $link=$this->input->post("link");
        $parent=$this->input->post("parent");
        $this->load->model("mmodule");
        if($this->mmodule->checkName($name) == false)
            echo "name";
        else {
            $data=array(
                "module_name"=>$name,
                "module_note"=>$note,
                "module_order"=>$order,
                "module_link"=>$link,
                "module_parent"=>$parent
            );
            $this->mmodule->insert($data);
            $this->session->set_flashdata("flash_mess", "Thêm module thành công.");
            echo "Thêm module thành công!";
        }
    }
    
    public function edit() {
        $this->_data["loadPage"]="module/edit_view02";
        $this->_data["titlePage"]="Chỉnh sửa module";
        
        //load data        
        $id=$this->uri->segment(4);
        $this->load->model("mmodule");
        $temp=$this->mmodule->getById($id);
        if($temp!=false)
            $this->_data["info_edit"]=$temp;
        $tmp=$this->mmodule->listAll();
        $this->load->helper("module_helper");
        if($tmp!=false)
            $this->_data["info_module"]=$tmp;
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtName",
                "label"=>"Tên",
                "rules"=>"callback_checkName"
            )
        );
        $this->form_validation->set_rules($config);
        
        //update
        if($this->form_validation->run()) {
            $data=array(
                "module_name"=>$this->input->post("txtName"),
                "module_note"=>$this->input->post("txtNote"),
                "module_order"=>$this->input->post("txtOrder"),
                "module_link"=>$this->input->post("txtLink"),
                "module_parent"=>$this->input->post("slModule")
            );
            $this->mmodule->update($data, $id);
            $this->session->set_flashdata("flash_mess", "Chỉnh sửa module thành công.");
            redirect(base_url()."admin_combo/module/");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function reloadData() {
        $this->load->model("mmodule");
        
        if($this->uri->segment(4)!=null) {
            $module_parent=$this->uri->segment(4);
            $this->_data["module_parent"]=$module_parent;
        }
        $this->load->model("mmodule");
        $this->load->helper("module_tree_helper");
        $tmp=$this->mmodule->listAll();
        if($tmp!=false) $this->_data["info"]=$tmp;
        
        $this->load->view("admin_combo/reload_view02", $this->_data);
    }
    
    public function add_bak() {
        $name=$this->input->post("name");
        $note=$this->input->post("note");
        $order=$this->input->post("order");
        $link=$this->input->post("link");
        $this->load->model("mmodule");
        if($this->mmodule->checkName($name) == false)
            echo "name";
        else {
            $data=array(
                "module_name"=>$name,
                "module_note"=>$note,
                "module_order"=>$order,
                "module_link"=>$link
            );
            $this->mmodule->insert($data);
            $this->session->set_flashdata("flash_mess", "Thêm module thành công.");
            echo "Thêm module thành công!";
        }
    }
    
    public function edit_bak() {
        $this->_data["loadPage"]="module/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa module";
        
        //load data
        $id=$this->uri->segment(4);
        $this->load->model("mmodule");
        $temp=$this->mmodule->getById($id);
        if($temp!=false)
            $this->_data["info_edit"]=$temp;
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtName",
                "label"=>"Tên",
                "rules"=>"callback_checkName"
            )
        );
        $this->form_validation->set_rules($config);
        
        //update
        if($this->form_validation->run()) {
            $data=array(
                "module_name"=>$this->input->post("txtName"),
                "module_note"=>$this->input->post("txtNote"),
                "module_order"=>$this->input->post("txtOrder"),
                "module_link"=>$this->input->post("txtLink")
            );
            $this->mmodule->update($data, $id);
            $this->session->set_flashdata("flash_mess", "Chỉnh sửa module thành công.");
            redirect(base_url()."admin_combo/module/");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("mmodule");
        $this->mmodule->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa module thành công.");
        redirect(base_url()."admin_combo/module/");
    }
    
    public function search() {
        $this->_data["loadPage"]="module/search_view";
        $this->_data["titlePage"]="Tìm kiếm module";
        
        $name=$this->session->userdata("sess_search_name");
        
        $this->load->model("mmodule");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/module/search/",
            "total_rows"=>$this->mmodule->countAllResults($name),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mmodule->search($name, $config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mmodule->search($name, $config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function searchdata() {
        if(isset($_POST["btnSearchModule"])) {
            $this->session->set_userdata("sess_search_name", $this->input->post("txtSearch"));
            redirect(base_url()."admin_combo/module/search");
        }
    }
    
    public function reloadData_bak() {
        $this->load->model("mmodule");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/module/index",
            "total_rows"=>$this->mmodule->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mmodule->listAllLimit($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mmodule->listAllLimit($config["per_page"], $start);
        
        $this->load->view("admin_combo/reload_view", $this->_data);
    }
    
    public function checkName($name) {
        $this->load->model("mmodule");
        $id=$this->uri->segment(4);
        if(!$this->mmodule->checkName($name, $id)) {
            $this->form_validation->set_message("checkName", "Tên này đã được đăng ký!");
            return false;
        }else
            return true;
    }
}
?>
